package com.yuhong.springbootinit.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuhong.springbootinit.common.BaseResponse;
import com.yuhong.springbootinit.common.ErrorCode;
import com.yuhong.springbootinit.common.ResultUtils;
import com.yuhong.springbootinit.exception.BusinessException;
import com.yuhong.springbootinit.mapper.InterviewMapper;
import com.yuhong.springbootinit.model.dto.interview.UpdateInterviewStatusRequest;
import com.yuhong.springbootinit.model.entity.Interview;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: resume-analysis-system
 * @BelongsPackage: com.yuhong.springbootinit.controller
 * @Author: renyuhong
 * @CreateTime: 2025-04-24  16:39
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/interview")
public class InterviewController {

    @Resource
    private InterviewMapper interviewMapper;

    /**
     * 获取某个用户的所有面试列表
     * @param userId 当前用户的 ID
     * @return 用户的面试列表
     */
    @GetMapping("/list/byUser")
    public BaseResponse<List<Interview>> listInterviewsByUserId(@RequestParam String userId) {
        if (StrUtil.isBlank(userId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "userId不能为空");
        }

        QueryWrapper<Interview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("id"); // 根据需要排序，比如最新在前

        List<Interview> interviews = interviewMapper.selectList(queryWrapper);

        return ResultUtils.success(interviews);
    }


    /**
     * @description: 用户确认面试流程
     * @author: renyuhong
     * @date: 2025/5/4 9:38
     * @param: [request]
     * @return: com.yuhong.springbootinit.common.BaseResponse<java.lang.Boolean>
     **/
    @PostMapping("/update/status")
    public BaseResponse<Boolean> updateInterviewStatus(@RequestBody UpdateInterviewStatusRequest request) {
        if (request == null || request.getId() == null || request.getStatus() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }

        Long id = request.getId();
        Integer status = request.getStatus();

        Interview interview = interviewMapper.selectById(id);
        if (interview == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "面试记录不存在");
        }

        Date now = new Date();
        interview.setStatus(status);

        switch (status) {
            case 3: interview.setTestDate(now); interview.setStatus(3); break;
            case 5: interview.setExamDate(now); interview.setStatus(5); break;
            case 7: interview.setInterviewDate(now); interview.setStatus(7); break;
            case 8: interview.setStatus(8); break; // 拒绝
            default:
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持的状态");
        }

        int rows = interviewMapper.updateById(interview);
        return ResultUtils.success(rows > 0);
    }



}